package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.DishService;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public void addCart(ShoppingCartDTO dto) {
        //创建shoppingcart对象
        ShoppingCart shoppingCart = new ShoppingCart();
        //拷贝属性值
        BeanUtils.copyProperties(dto,shoppingCart);//浅拷贝
        ///判断该商品是否已经存在于购物车中：dishId+userId+dishFlavor
        //只能查本人的
        shoppingCart.setUserId(BaseContext.getCurrentId());
        ShoppingCart cart=shoppingCartMapper.selectBy(shoppingCart);
        if(cart==null){//代表购物车没有该商品数据
            //补充缺失的属性值
            //判断是新增套餐还是新增单品
            if (dto.getDishId() != null)//代表新增单品
            {
                //根据菜品id查询菜品信息
                Dish dish = dishMapper.selectById(dto.getDishId());
                shoppingCart.setName(dish.getName());
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setImage(dish.getImage());
            }
            else//代表新增套餐
            {
                Setmeal setmeal = setmealMapper.getById(dto.getSetmealId());
                //根据菜品id查询菜品信息
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCart.setImage(setmeal.getImage());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);
        }else//代表有该商品数据
        {
            //将原来的数量加1
            cart.setNumber(cart.getNumber()+1);
            shoppingCartMapper.update(cart);
        }


        //最终目的：将用户添加的商品信息保存到购物车shopping_cart表中
    }

    @Override
    public List<ShoppingCart> list() {
        //根据当前用户id查询购物车中所有商品
        return shoppingCartMapper.list(BaseContext.getCurrentId());
    }

    //删除购物车中商品
    @Override
    public void clean() {
        //根据当前用户id清空购物车
        shoppingCartMapper.deleteByUserId(BaseContext.getCurrentId());
    }

    // 删除购物车中单个商品
    @Override
    public void sub(ShoppingCartDTO dto) {
        // 根据传入的dto创建一个购物车对象，并设置当前用户id
        ShoppingCart cartForSearch = new ShoppingCart();
        BeanUtils.copyProperties(dto, cartForSearch);
        cartForSearch.setUserId(BaseContext.getCurrentId());

        // 使用selectBy方法精确查询符合条件的商品
        ShoppingCart targetCart = shoppingCartMapper.selectBy(cartForSearch);

        if (targetCart != null) {
            Integer number = targetCart.getNumber();
            if (number == 1) {
                // 如果数量为1，则删除该商品
                shoppingCartMapper.deleteById(targetCart.getId());
            } else {
                // 否则数量减一并更新
                targetCart.setNumber(number - 1);
                shoppingCartMapper.update(targetCart);
            }
        }
    }
}
